import { defineStore } from 'pinia'
import { store } from '@/stores'
import {getUserInfo} from "../api/user.js";
import {LOGIN_STATUS,UID,PLATFORM,IS_LOGIN} from '../config/cache.js';
import Cache from '../utils/cache.js';
import {USER_INFO} from '../config/cache.js';
import { getMerTypeListApi, getMerCategoryListApi} from '@/api/merchant.js';


  export const useAppStore = defineStore({
	id: 'app',
	state: () => ({
		token: Cache.get(LOGIN_STATUS) || '',
		backgroundColor: "#fff",
		userInfo: Cache.get(USER_INFO)?JSON.parse(Cache.get(USER_INFO)):{},
		uid: Cache.get(UID) || null,
		homeActive: false,
		chatUrl: Cache.get('chatUrl') || '',
		systemPlatform: Cache.get(PLATFORM)?Cache.get(PLATFORM):'',
		productType: Cache.get('productType') || '',
		merchantClassify: Cache.get('merchantClassify')?JSON.parse(Cache.get('merchantClassify')) : [] ,  /** 商户分类 **/
		merchantType: Cache.get('merchantType')?JSON.parse(Cache.get('merchantType')) : [], /** 商户类型 **/
		merchantAPPInfo: Cache.get('merchantAPPInfo') ? JSON.parse(Cache.get('merchantAPPInfo')) : {},
		theme:Cache.get('merchantAPPInfo'),
		navActive:'0',
		currency:'$',/** 币种 **/
		isLogin:Cache.get(IS_LOGIN) || '',
		tabBar: [
			'/pages/index/index',
			'/pages/goods_cate/index',
			'/pages/order_addcart/order_addcart',
			'/pages/user/index'
		],
		navHeight:0,
		statusBarHeight:0
	}),
	actions: {
		LOGIN( opt) {
			this.token = opt.token;
			this.isLogin = true
			Cache.set(LOGIN_STATUS, opt.token);
			Cache.set(IS_LOGIN, true);
		},
		SETUID(val){                                                
			this.uid = val;
			Cache.set(UID, val);
		},
		UPDATE_LOGIN( token) {
			this.token = token;
		},
		LOGOUT() {
			this.token = undefined;
			this.uid = undefined
			this.isLogin = false
			Cache.clear(LOGIN_STATUS);
			Cache.clear(UID);
			Cache.clear(USER_INFO);
			Cache.clear(IS_LOGIN);
		},
		BACKGROUND_COLOR( color) {
			this.color = color;
			document.body.style.backgroundColor = color;
		},
		UPDATE_USERINFO( userInfo) {
			this.userInfo = userInfo;
			Cache.set(USER_INFO, userInfo);
		},
		OPEN_HOME() {
			this.homeActive = true;
		},
		CLOSE_HOME() {
			this.homeActive = false;
		},
		SET_CHATURL( chatUrl){
			this.chatUrl = chatUrl;
		},
		SYSTEM_PLATFORM( systemPlatform){
			this.systemPlatform = systemPlatform;
			Cache.set(PLATFORM, systemPlatform);
		},
		//更新useInfo数据
		changInfo( payload) {
			this.userInfo[payload.amount1] = payload.amount2;
			Cache.set(USER_INFO, this.userInfo);
		},
		//商品类型，用于区分视频号商品与一般商品
		PRODUCT_TYPE( productType) {
			this.productType = productType;
			Cache.set('productType', productType);
		},
		/** 商户全部分类  **/
		SET_MerchantClassify( merchantClassify) {
			this.merchantClassify = changeNodes(merchantClassify)
			Cache.set('merchantClassify', JSON.stringify(changeNodes(merchantClassify)));
		},
		 /** 商户全部类型 **/
		SET_MerchantType ( merchantType){
			this.merchantType = changeNodes(merchantType)
			Cache.set('merchantType', JSON.stringify(changeNodes(merchantType)));
		},
		 /** 商户信息 **/
		MERCHANTJINFO( merchantJInfo){
			this.merchantAPPInfo = merchantJInfo
			Cache.set('merchantAPPInfo', merchantJInfo);
		},
		SET_THEME (theme){
			this.theme = theme
			Cache.set('theme', theme);
		},
		SET_NAVACTIVE (index){
			this.navActive = index
		},
		USERINFO( force) {
			return new Promise(reslove => {
				getUserInfo().then(res => {
					this.UPDATE_USERINFO(res.data)
					reslove(res.data);
				});
			}).catch(err => {
				return that.$util.Tips({
					title: err
				});
			});
		},
		MerCategoryList() {
			return new Promise(reslove => {
				getMerCategoryListApi().then(res => {
					this.SET_MerchantClassify(res.data)
					reslove(res.data);
				});
			}).catch(err => {
				return that.$util.Tips({
					title: err
				});
			});
		},
		MerTypeList() {
			return new Promise(reslove => {
				getMerTypeListApi().then(res => {
					this.SET_MerchantType(res.data)
					reslove(res.data);
				});
			}).catch(err => {
				return that.$util.Tips({
					title: err
				});
			});
		}
	},
	// persist: {
	// 	enabled: true, // 开启数据缓存
	// 	strategies: [{
	// 		// 自定义存储的 key，默认是 store.$id
	// 		key: "useAppStore",
	// 		// 可以指定任何 extends Storage 的实例，默认是 sessionStorage
	// 		storage: localStorage,
	// 		// state 中的字段名，按组打包储存
	// 		// paths: ["foo", "bar"]
	// 	}]
	//   }
  })
  

/** tree去除 childList=[] 的结构**/
const changeNodes = function(data) {
	if (data.length > 0) {
	  for (var i = 0; i < data.length; i++) {
		if (!data[i].childList || data[i].childList.length < 1) {
		  data[i].childList = undefined;
		} else {
		  changeNodes(data[i].childList);
		}
	  }
	}
	return data
  };

  export function useAppStoreWithOut () {
	return useAppStore(store)
  }